package org.jet.emall.prod.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jet.emall.common.restful.result.CommonPage;
import org.jet.emall.prod.dto.OrderDTO;
import org.jet.emall.prod.po.OrderItemDetailView;
import org.jet.emall.prod.po.OrderMaster;
import org.jet.emall.prod.service.mp.IOrderItemDetailViewService;
import org.jet.emall.prod.service.mp.IOrderMasterService;
import org.jet.emall.prod.transfer.OrderDtoTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-05-02 12:02
 */

@Service
public class CustomOrderServiceImpl implements CustomOrderService {

    @Autowired
    private IOrderMasterService orderMasterService;

    @Autowired
    IOrderItemDetailViewService orderItemDetailViewService;




    @Override
    public CommonPage<OrderDTO> page(String code, String shippingUser, String shippingPhone, String shippingAddress,Byte status,Long minMoney,Long maxMoney, Integer pageNum, Integer pageSize) {
        IPage<OrderMaster> page = new Page<>();
        page.setCurrent(pageNum).setSize(pageSize);

        Wrapper<OrderMaster> wrapper = new QueryWrapper<>();
        ((QueryWrapper<OrderMaster>) wrapper).like(StringUtils.isNotBlank(code),"code",code)
                .like(StringUtils.isNotBlank(shippingUser),"shipping_user",shippingUser)
                .like(StringUtils.isNotBlank(shippingPhone),"shipping_phone",shippingPhone);
        if(minMoney!=null) {
            ((QueryWrapper<OrderMaster>) wrapper).ge(ObjectUtils.isNotNull(minMoney), "order_money", minMoney * 100);
        }
        if(maxMoney!=null) {
            ((QueryWrapper<OrderMaster>) wrapper).le(ObjectUtils.isNotNull(maxMoney), "order_money", maxMoney * 100);
        }

        if(status!=null){
            ((QueryWrapper<OrderMaster>) wrapper).eq("status",status);
        }
        if(StringUtils.isNotBlank(shippingAddress)){
            ((QueryWrapper<OrderMaster>) wrapper).like("province",shippingAddress)
                    .or().like("city",shippingAddress)
                    .or().like("district",shippingAddress)
                    .or().like("address",shippingAddress);
        }
        orderMasterService.page(page, wrapper);
        CommonPage<OrderDTO> commonPage = new CommonPage<OrderDTO>().setTotalPage(page.getPages())
                .setTotal(page.getTotal()).setPageNum(page.getCurrent()).setPageSize(page.getSize())
                .setList(OrderDtoTransfer.INSTANCE.toDtoList(page.getRecords()));
        return commonPage;
    }

    @Override
    public OrderDTO getOrderById(Long id) {
        OrderMaster orderMaster = orderMasterService.getById(id);
        OrderDTO orderDTO = OrderDtoTransfer.INSTANCE.toDto(orderMaster);
        List<OrderItemDetailView> itemDetailViews = orderItemDetailViewService.list(new QueryWrapper<OrderItemDetailView>().eq("order_id", orderDTO.getId()));
        orderDTO.setItems(OrderDtoTransfer.INSTANCE.toItemDtoList(itemDetailViews));
        return orderDTO;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Long id) {
        orderMasterService.removeById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(String code, Byte status) {
        orderMasterService.update(new UpdateWrapper<OrderMaster>().set("status",status).eq("code",code));
    }


}
